﻿Imports System.Reflection
Imports Microsoft.Office.Interop

Public Class MainForm

    Private Sub btnClean_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClean.Click
        If DialogResult.OK = MessageBox.Show("Are you sure you want to delete all of the Contact photos from Outlook?", Application.ProductName, MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) Then
            CleanContactPictures(cboContactsFolder.SelectedItem)
        End If
    End Sub

    Private Sub CleanContactPictures(ByVal contactsFolder As Outlook.MAPIFolder)
        Try
            Cursor.Current = Cursors.WaitCursor

            Dim myContacts As Outlook.Items
            Dim myItem As Object
            Dim removedPics, totalCount As Integer

            myContacts = contactsFolder.Items
            removedPics = 0
            totalCount = 0
            For Each myItem In myContacts
                Dim myContact As Outlook.ContactItem
                myContact = CType(myItem, Outlook.ContactItem)
                If (Not myContact Is Nothing) Then
                    totalCount += 1
                    If myContact.HasPicture Then
                        myContact.RemovePicture()
                        myContact.Save()
                        removedPics += 1
                    End If
                End If
            Next
            Cursor.Current = Cursors.Default
            MessageBox.Show(String.Format("Found and removed {0} pictures across {1} contacts.", removedPics, totalCount), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            Cursor.Current = Cursors.Default
            MessageBox.Show("An error occured: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadContactsFolders(cboContactsFolder)
    End Sub

    Private Sub LoadContactsFolders(ByVal comboBox As ComboBox)
        Try
            Cursor.Current = Cursors.WaitCursor

            Dim myOlApp As Outlook.Application
            Dim myNamespace As Outlook.NameSpace

            myOlApp = CreateObject("Outlook.Application")
            myNamespace = myOlApp.GetNamespace("MAPI")

            Dim folders As Outlook.MAPIFolder = myNamespace.DefaultStore.GetRootFolder
            Dim folder As Outlook.MAPIFolder
            For Each folder In folders.Folders
                If folder.DefaultItemType = Outlook.OlItemType.olContactItem Then
                    comboBox.Items.Add(folder)
                End If
            Next
            If comboBox.Items.Count > 0 Then cboContactsFolder.SelectedIndex = 0
            Cursor.Current = Cursors.Default
        Catch ex As Exception
            Cursor.Current = Cursors.Default
            MessageBox.Show("An error occured reading all the contact folders: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

End Class
